package git.bankview.wl.bean;

import java.util.*;
import java.text.*;
import java.sql.*;
import java.io.*;

import git.bankview.sl.db.*;

import org.apache.struts.action.ActionForm;
import org.apache.log4j.Logger;

public class TransNavigateBean
{
	private static Logger log = Logger.getLogger(TransNavigateBean.class);

	private String m_oTransid = null;
	private String m_oTranslabel = null;
	private InfoTransBean[] m_vNavigateList = null;
	
	public void setTransid(String val)
	{
		m_oTransid = val;
	}
	
	public String getTransid()
	{
		return m_oTransid;
	}
	
	public String getTranslabel()
		throws Exception
	{
		if (null == m_oTranslabel)
			validate();
		
		return m_oTranslabel;
	}
	
	public String getNavigatePath()
		throws Exception
	{
		if (null == m_vNavigateList)
			validate();
		
		StringBuffer buff = new StringBuffer();
		
		for (int i = 0; i < m_vNavigateList.length; ++i)
		{
			if (0 < i)
				buff.append(">>");
			buff.append(m_vNavigateList[i].getTranslabel());
		}
		
		return buff.toString();
	}
	
	public InfoTransBean[] getNavigateList()
		throws Exception
	{
		if (null == m_vNavigateList)
			validate();
		
		return m_vNavigateList;
	}
	
	private void validate()
		throws Exception
	{
		Connection oConn = DBConnectionFactory.getCoreConnection();
		try
		{
			PreparedStatement ps = oConn.prepareStatement("select * from infotrans where transid = ?");
			String currTransid = m_oTransid;
			
			Vector v = new Vector();
			for (int i = 0; !"00000".equals(currTransid); ++i)
			{
				ps.setString(1, currTransid);
				ResultSet rs = ps.executeQuery();
				rs.next();
				InfoTransBean c = new InfoTransBean(rs);
				
				if (0 == i)
					m_oTranslabel = c.getTranslabel();
				
				currTransid = c.getPtransid();
				v.addElement(c);
			}
			
			m_vNavigateList = new InfoTransBean[v.size()];
			for (int i = 0; i < m_vNavigateList.length; ++i)
			{
				m_vNavigateList[i] = (InfoTransBean)v.get(m_vNavigateList.length - i - 1);
			}
		}
		catch(Exception e)
		{
			log.warn("can not get navigate info of trans: ", e);
			throw e;
		}
		finally
		{
			oConn.close();
		}
	}
}
